
const getXYByDirection = (direction: 'toLeft' | 'toRight' | 'toTop' | 'toBottom') => {
    if (direction === 'toRight') {
        return {x: 1, y: 0, x2: 0, y2: 0};
    } else if (direction === 'toLeft') {
        return {x: 0, y: 0, x2: 1, y2: 0};
    } else if (direction === 'toBottom') {
        return {x: 0, y: 0, x2: 0, y2: 1};
    } else if (direction === 'toTop') {
        return {x: 0, y: 1, x2: 0, y2: 0};
    }
    return {x: 0, y: 0, x2: 0, y2: 1};
}

// 构建一个ECharts的color实例：线性渐变
export const buildLinearGradient = (direction: 'toLeft' | 'toRight' | 'toTop' | 'toBottom', startColor: string, endColor: string): any => {
    return {
        type: 'linear',
        ...getXYByDirection(direction),
        colorStops: [{
            offset: 0, color: startColor, // 0% 处的颜色
        }, {
            offset: 1, color: endColor, // 100% 处的颜色
        }],
    };
}

// 构建一个ECharts的color实例：径向渐变
export const buildRadialGradient = (x: number, y: number, r: number, startColor: string, endColor: string): any => {
    return {
        type: 'radial',
        x, y, r,
        colorStops: [{
            offset: 0, color: startColor, // 0% 处的颜色
        }, {
            offset: 1, color: endColor, // 100% 处的颜色
        }],
    };
}